<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>EmailErrorReporter.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #000000; background-color: #ffffff; font-family: Monospaced}
table {color: #000000; background-color: #e9e8e2; font-family: Monospaced}
.java-layer-method {font-family: Monospaced; font-weight: bold}
.java-keywords {color: #000099; font-family: Monospaced; font-weight: bold}
.java-char-literal {color: #006f00}
.java-block-comment {color: #737373}
.java-string-literal {color: #99006b}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/Users/richardallenbair/Documents/Source/Projects/nonsense/swingx/src/java/org/jdesktop/swingx/error/EmailErrorReporter.java</td></tr></table>
<pre>
<span class="java-block-comment">/*</span>
<span class="java-block-comment"> * $Id$</span>
<span class="java-block-comment"> *</span>
<span class="java-block-comment"> * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,</span>
<span class="java-block-comment"> * Santa Clara, California 95054, U.S.A. All rights reserved.</span>
<span class="java-block-comment"> *</span>
<span class="java-block-comment"> * This library is free software; you can redistribute it and/or</span>
<span class="java-block-comment"> * modify it under the terms of the GNU Lesser General Public</span>
<span class="java-block-comment"> * License as published by the Free Software Foundation; either</span>
<span class="java-block-comment"> * version 2.1 of the License, or (at your option) any later version.</span>
<span class="java-block-comment"> * </span>
<span class="java-block-comment"> * This library is distributed in the hope that it will be useful,</span>
<span class="java-block-comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="java-block-comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span>
<span class="java-block-comment"> * Lesser General Public License for more details.</span>
<span class="java-block-comment"> * </span>
<span class="java-block-comment"> * You should have received a copy of the GNU Lesser General Public</span>
<span class="java-block-comment"> * License along with this library; if not, write to the Free Software</span>
<span class="java-block-comment"> * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA</span>
<span class="java-block-comment"> */</span>

<span class="java-keywords">package</span> org.jdesktop.swingx.error;

<span class="java-keywords">import</span> java.io.PrintWriter;
<span class="java-keywords">import</span> java.io.StringWriter;
<span class="java-keywords">import</span> java.util.ArrayList;
<span class="java-keywords">import</span> java.util.Arrays;
<span class="java-keywords">import</span> java.util.List;
<span class="java-keywords">import</span> org.jdesktop.swingx.*;
<span class="java-keywords">import</span> org.jdesktop.swingx.util.SwingWorker;


<span class="java-block-comment">/**</span>
<span class="java-block-comment"> * &lt;p&gt;A convenient base class for email based {@link ErrorReporter} implementations.</span>
<span class="java-block-comment"> * &lt;code&gt;EmailErrorReporter&lt;/code&gt; extends {@link JavaBean}, and thus contains</span>
<span class="java-block-comment"> * all the infrastructure for firing property change events.&lt;/p&gt;</span>
<span class="java-block-comment"> *</span>
<span class="java-block-comment"> * &lt;p&gt;&lt;code&gt;EmailErrorReporter&lt;/code&gt; allows developers to specify an array of</span>
<span class="java-block-comment"> * &quot;to&quot; addresses, &quot;cc&quot; addresses, and &quot;bcc&quot; addresses. It also supports an</span>
<span class="java-block-comment"> * arbitrary number of attachments. Typically, the subject and message parts of</span>
<span class="java-block-comment"> * the email are constructed automatically by subclasses. Hence, the</span>
<span class="java-block-comment"> * {@link #constructMessageBody(IncidentInfo)} and </span>
<span class="java-block-comment"> * {@link constructSubject(IncidentInfo)} protected methods. Subclasses</span>
<span class="java-block-comment"> * should override these methods if they want to change the default message or</span>
<span class="java-block-comment"> * subject generated by this class.&lt;/p&gt;</span>
<span class="java-block-comment"> *</span>
<span class="java-block-comment"> * &lt;p&gt;Subclasses must implement the {@link sendMessage()} method. This is the</span>
<span class="java-block-comment"> * method that performs the actual send task. Implementations of this method</span>
<span class="java-block-comment"> * need not worry about blocking the method. This class will ensure that</span>
<span class="java-block-comment"> * {@link sendMessage} is called on a background thread.&lt;/p&gt;</span>
<span class="java-block-comment"> *</span>
<span class="java-block-comment"> * @author Alexander Zuev</span>
<span class="java-block-comment"> * @author rbair</span>
<span class="java-block-comment"> * @version 1.1</span>
<span class="java-block-comment"> */</span>
<span class="java-keywords">public</span> <span class="java-keywords">abstract</span> <span class="java-keywords">class</span> EmailErrorReporter <span class="java-keywords">extends</span> JavaBean <span class="java-keywords">implements</span> ErrorReporter {
    <span class="java-keywords">private</span> List&lt;String&gt; toList = <span class="java-keywords">new</span> ArrayList&lt;String&gt;();
    <span class="java-keywords">private</span> List&lt;String&gt; ccList = <span class="java-keywords">new</span> ArrayList&lt;String&gt;();
    <span class="java-keywords">private</span> List&lt;String&gt; bccList = <span class="java-keywords">new</span> ArrayList&lt;String&gt;();
    <span class="java-keywords">private</span> List&lt;Object&gt; attachments = <span class="java-keywords">new</span> ArrayList&lt;Object&gt;();
    
    <span class="java-block-comment">/**</span>
<span class="java-block-comment">     * Creates a new &lt;code&gt;EmailErrorReporter&lt;/code&gt;.</span>
<span class="java-block-comment">     */</span>
    <span class="java-keywords">public</span> <span class="java-layer-method">EmailErrorReporter</span>() {
        <span class="java-block-comment">//...</span>
    }
    
    <span class="java-keywords">public</span> <span class="java-keywords">void</span> <span class="java-layer-method">setToAddresses</span>(<span class="java-keywords">int</span> index, String to) {}
    <span class="java-keywords">public</span> String <span class="java-layer-method">getToAddresses</span>(<span class="java-keywords">int</span> index) {<span class="java-keywords">return</span> <span class="java-keywords">null</span>;}
    <span class="java-keywords">public</span> <span class="java-keywords">void</span> <span class="java-layer-method">setToAddresses</span>(String[] addresses) {
        toList.<span class="java-layer-method">clear</span>();
        toList.<span class="java-layer-method">addAll</span>(Arrays.<span class="java-layer-method">asList</span>(addresses));
    }
    <span class="java-keywords">public</span> String[] <span class="java-layer-method">getToAddresses</span>() {<span class="java-keywords">return</span> <span class="java-keywords">null</span>;}
    
    <span class="java-keywords">public</span> <span class="java-keywords">void</span> <span class="java-layer-method">setCcAddresses</span>(<span class="java-keywords">int</span> index, String to) {}
    <span class="java-keywords">public</span> String <span class="java-layer-method">getCcAddresses</span>(<span class="java-keywords">int</span> index) {<span class="java-keywords">return</span> <span class="java-keywords">null</span>;}
    <span class="java-keywords">public</span> <span class="java-keywords">void</span> <span class="java-layer-method">setCcAddresses</span>(String[] addresses) {}
    <span class="java-keywords">public</span> String[] <span class="java-layer-method">getCcAddresses</span>() {<span class="java-keywords">return</span> <span class="java-keywords">null</span>;}

    <span class="java-keywords">public</span> <span class="java-keywords">void</span> <span class="java-layer-method">setBccAddresses</span>(<span class="java-keywords">int</span> index, String to) {}
    <span class="java-keywords">public</span> String <span class="java-layer-method">getBccAddresses</span>(<span class="java-keywords">int</span> index) {<span class="java-keywords">return</span> <span class="java-keywords">null</span>;}
    <span class="java-keywords">public</span> <span class="java-keywords">void</span> <span class="java-layer-method">setBccAddresses</span>(String[] addresses) {}
    <span class="java-keywords">public</span> String[] <span class="java-layer-method">getBccAddresses</span>() {<span class="java-keywords">return</span> <span class="java-keywords">null</span>;}
    
    <span class="java-keywords">public</span> <span class="java-keywords">void</span> <span class="java-layer-method">setAttachments</span>(<span class="java-keywords">int</span> index, String to) {}
    <span class="java-keywords">public</span> String <span class="java-layer-method">getAttachments</span>(<span class="java-keywords">int</span> index) {<span class="java-keywords">return</span> <span class="java-keywords">null</span>;}
    <span class="java-keywords">public</span> <span class="java-keywords">void</span> <span class="java-layer-method">setAttachments</span>(String[] addresses) {}
    <span class="java-keywords">public</span> String[] <span class="java-layer-method">getAttachments</span>() {<span class="java-keywords">return</span> <span class="java-keywords">null</span>;}
    
    <span class="java-block-comment">/**</span>
<span class="java-block-comment">     * @inheritDoc</span>
<span class="java-block-comment">     */</span>
    <span class="java-keywords">public</span> <span class="java-keywords">void</span> <span class="java-layer-method">reportIncident</span>(IncidentInfo info) {
        SendWorker thread = <span class="java-keywords">new</span> <span class="java-layer-method">SendWorker</span>(<span class="java-keywords">this</span>, info);
        thread.<span class="java-layer-method">execute</span>();
    }

    <span class="java-block-comment">/**</span>
<span class="java-block-comment">     * Gets the message</span>
<span class="java-block-comment">     * This method is used to extract text message from the provided &lt;code&gt;IncidentInfo&lt;/code&gt;.</span>
<span class="java-block-comment">     * Override this method to change text formatting or contents.</span>
<span class="java-block-comment">     *</span>
<span class="java-block-comment">     * @param incident - Incapsulates all the information about error</span>
<span class="java-block-comment">     * @return String to be used as a body message in report.</span>
<span class="java-block-comment">     */</span>
    <span class="java-keywords">protected</span> String <span class="java-layer-method">constructMessageBody</span>(IncidentInfo incident) {
        String body = incident.<span class="java-layer-method">getBasicErrorMessage</span>();
        <span class="java-keywords">if</span>(incident.<span class="java-layer-method">getDetailedErrorMessage</span>() != <span class="java-keywords">null</span>) {
            body.<span class="java-layer-method">concat</span>(<span class="java-string-literal">&quot;\n&quot;</span>+incident.<span class="java-layer-method">getDetailedErrorMessage</span>());
        }
        <span class="java-keywords">if</span>(incident.<span class="java-layer-method">getErrorException</span>() != <span class="java-keywords">null</span>) {
            StringWriter sw = <span class="java-keywords">new</span> <span class="java-layer-method">StringWriter</span>();
            PrintWriter pw = <span class="java-keywords">new</span> <span class="java-layer-method">PrintWriter</span>(sw);
            incident.<span class="java-layer-method">getErrorException</span>().<span class="java-layer-method">printStackTrace</span>(pw);
            body = body + <span class="java-string-literal">&quot;\n ----- &quot;</span> + sw.<span class="java-layer-method">toString</span>();
        }
        <span class="java-keywords">return</span> body;
    }
    
    <span class="java-keywords">protected</span> String <span class="java-layer-method">constructSubject</span>(IncidentInfo incident) {
        <span class="java-keywords">return</span> incident.<span class="java-layer-method">getHeader</span>();
    }

    <span class="java-block-comment">/**</span>
<span class="java-block-comment">     * Compose and send message</span>
<span class="java-block-comment">     * @param info</span>
<span class="java-block-comment">     */</span>
    <span class="java-keywords">protected</span> <span class="java-keywords">abstract</span> <span class="java-keywords">void</span> <span class="java-layer-method">sendMessage</span>(IncidentInfo info);
    
    <span class="java-block-comment">/**</span>
<span class="java-block-comment">     * A very simple SwingWorker for performing the &quot;send&quot; operation on a background</span>
<span class="java-block-comment">     * thread. This may be expanded in the future to allow a GUI showing the </span>
<span class="java-block-comment">     * process of the send operation.</span>
<span class="java-block-comment">     */</span>
    <span class="java-keywords">private</span> <span class="java-keywords">static</span> <span class="java-keywords">final</span> <span class="java-keywords">class</span> SendWorker <span class="java-keywords">extends</span> SwingWorker {
        <span class="java-keywords">private</span> EmailErrorReporter reporter;
        <span class="java-keywords">private</span> IncidentInfo info;
        
        <span class="java-keywords">private</span> <span class="java-layer-method">SendWorker</span>(EmailErrorReporter r, IncidentInfo i) {
            <span class="java-keywords">if</span> (r == <span class="java-keywords">null</span> || i == <span class="java-keywords">null</span>) {
                <span class="java-keywords">throw</span> <span class="java-keywords">new</span> <span class="java-layer-method">NullPointerException</span>(<span class="java-string-literal">&quot;Both EmailErrorReporter and &quot;</span> +
                        <span class="java-string-literal">&quot;IncidentInfo must be specified&quot;</span>);
            }
            <span class="java-keywords">this</span>.reporter = r;
            <span class="java-keywords">this</span>.info = i;
        }
        
        <span class="java-char-literal">@</span>Override
        <span class="java-keywords">protected</span> Object <span class="java-layer-method">doInBackground</span>() <span class="java-keywords">throws</span> Exception {
            reporter.<span class="java-layer-method">sendMessage</span>(info);
            <span class="java-keywords">return</span> <span class="java-keywords">null</span>;
        }
    }
}

</pre></body>
</html>
